package com.ui.home.view;

import android.content.Context;
import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.base.BaseRefreshFragment;
import com.bean.BannerBean;
import com.bean.BaseBean;
import com.bean.HomeArticleList;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.ddd.wandemo.ArticleDetailWebView;
import com.ddd.wandemo.ArticleDetailWebView2;
import com.ddd.wandemo.MainActivity;
import com.ddd.wandemo.R;
import com.ui.TestArticleRvAdapter2;
import com.ui.home.presenter.HomeFragmentContract;
import com.ui.home.presenter.HomeFragmentPresenter;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

public class HomeArticleFragment extends BaseRefreshFragment<HomeFragmentPresenter> implements HomeFragmentContract.View{
    private int page = 0;
    private List<HomeArticleList.DatasBean> datas = new ArrayList<>();
    private RecyclerView mRvHomeArticle;
    private TestArticleRvAdapter2 mAdapter;
    private SwipeRefreshLayout mSrlHomeArticle;
    private Banner mBanner;
    private String[] homeLink = {"http://www.wanandroid.com/blog/show/2090","http://www.wanandroid.com/blog/show/2245",
            "http://www.wanandroid.com/article/query?k=%E9%80%9F%E6%9F%A5","http://www.wanandroid.com/openapis"};
    private List<String> mBannerUrl;
    private List<String> mBannerTitles;

    public static HomeArticleFragment newInstance() {
        HomeArticleFragment fragment = new HomeArticleFragment();
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragmet_home_article;
    }

    @Override
    protected void initEventAndData() {
        //刷新回调
        mSrlHomeArticle.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 0;
                mPresenter.getArticle(page);
            }
        });

        //加载回调
        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                mPresenter.getArticle(++page);
            }
        }, mRvHomeArticle);



        //item点击回调
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                HomeArticleList.DatasBean data = (HomeArticleList.DatasBean) adapter.getData().get(position);
                Intent intent = new Intent(getActivity(), ArticleDetailWebView2.class);
                intent.putExtra("link",data.getLink());
                intent.putExtra("title",data.getTitle());
                intent.putExtra("type",ArticleDetailWebView.TYPE_ARTICLE);
                startActivity(intent);
            }
        });

    }

    @Override
    protected void initView() {
        mRvHomeArticle = view.findViewById(R.id.rv_article_list);
        mSrlHomeArticle = view.findViewById(R.id.srl_home_article);

        mSrlHomeArticle.setColorSchemeColors(((MainActivity)getActivity()).mAccentColor);
        mRvHomeArticle.setLayoutManager(new LinearLayoutManager(mContext));

        mAdapter = new TestArticleRvAdapter2(R.layout.item_home_article, datas);

        View bannerView = LayoutInflater.from(mContext).inflate(R.layout.item_home_banner, null, false);
        initBanner(bannerView);

        //添加头部banner
        mAdapter.addHeaderView(bannerView);
        mRvHomeArticle.setAdapter(mAdapter);
    }


    private void initBanner(View bannerView) {
        mBanner = bannerView.findViewById(R.id.banner_home);
        mBanner.setImageLoader(new GlideImageLoader());
//        mBanner.setBannerAnimation(Transformer.ZoomIn);
        //设置自动轮播，默认为true
        mBanner.isAutoPlay(true);
        //设置轮播时间
        mBanner.setDelayTime(5000);
        //设置指示器位置（当banner模式中有指示器时）
        mBanner.setIndicatorGravity(BannerConfig.RIGHT);
        mBanner.setBannerStyle(BannerConfig.NUM_INDICATOR_TITLE	);

        //下面4个图标
        LinearLayout llKnowledge = bannerView.findViewById(R.id.ll_home_four_knowledge);

        for (int i =0;i<llKnowledge.getChildCount();i++){
            final int index = i;
            llKnowledge.getChildAt(i).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getActivity(), ArticleDetailWebView2.class);
                    intent.putExtra("link",homeLink[index]);
                    intent.putExtra("title","详情");
                    intent.putExtra("type",ArticleDetailWebView.TYPE_HOME);
                    startActivity(intent);
                }
            });
        }

        mBanner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                Intent intent = new Intent(getActivity(), ArticleDetailWebView2.class);
                intent.putExtra("link",mBannerUrl.get(position));
                intent.putExtra("title",mBannerTitles.get(position));
                intent.putExtra("type",ArticleDetailWebView.TYPE_HOME);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void loadData() {
        mPresenter.getArticle(page);
        mPresenter.getBannerList();
    }

    @Override
    protected HomeFragmentPresenter initPresenter() {
        return new HomeFragmentPresenter();
    }

    @Override
    public void onGetArticle(List<HomeArticleList.DatasBean> articleList) {
        mAdapter.setNewData(articleList);
        //首次进入不走loadmore回调
        mAdapter.disableLoadMoreIfNotFullPage();
        mSrlHomeArticle.setRefreshing(false);
    }

    @Override
    public void onLoadMore(List<HomeArticleList.DatasBean> articleList) {
        if (articleList.size() == 0) {
            mAdapter.loadMoreEnd();
        } else {
            mAdapter.addData(articleList);
            mAdapter.loadMoreComplete();
        }
    }

    @Override
    public void onGetBannerList(BaseBean<List<BannerBean>> bannerBeanBaseBean) {
        List<String> images = new ArrayList<>();
        mBannerTitles = new ArrayList<>();
        mBannerUrl = new ArrayList<>();
        for (BannerBean dataBean : bannerBeanBaseBean.getData()) {
            images.add(dataBean.getImagePath());
            mBannerTitles.add(dataBean.getTitle());
            mBannerUrl.add(dataBean.getUrl());
        }
        mBanner.setImages(images);
        mBanner.setBannerTitles(mBannerTitles);
        mBanner.start();
    }

    //banner图片加载器
    class GlideImageLoader extends ImageLoader {
        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {
            Glide.with(context).load(path).into(imageView);
        }

//        @Override
//        public ImageView createImageView(Context context) {
//            CustomRoundAngleImageView imageView = new CustomRoundAngleImageView(mContext);
//            return imageView;
//        }
    }
}
